package satir.card.factory;

import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import satir.card.ActCard;
import satir.card.Card;
import satir.card.Choice;
import satir.card.Question;

/**
 * A factory for creating ActCard objects.
 * 
 * @version 2.0 March 2012
 * @author Antony Chan
 */
public class ActCardFactory extends CardFactory {

	/**
	 * Break down the structure of cardJson to retrieve required data
	 * And then create Choice, Question, ActCard accordingly
	 * @return Instance of ActCard
	 */
	@Override
	public Card createCard() {
		String name = "";
		ArrayList<Question> questions = new ArrayList<Question>();
		JSONArray questionsJson;
		try {
			name = cardJson.getString("name");
			questionsJson = cardJson.getJSONArray("questions");
			for(int i=0; i<questionsJson.length(); i++){			
				JSONObject questionJson = questionsJson.getJSONObject(i);
				JSONArray choiceJsonArr = questionJson.getJSONArray("choice");
				ArrayList<Choice> choices = new ArrayList<Choice>();
				for(int j=0; j<choiceJsonArr.length(); j++){
					JSONObject choiceJson = choiceJsonArr.getJSONObject(j);
					Hashtable<String, Integer> factorTmp = new Hashtable<String, Integer>();
					factorTmp.put(choiceJson.getString("factor"), choiceJson.getInt("score"));
					choices.add(new Choice(choiceJson.getString("content"), choiceJson.getInt("rewardSE"), factorTmp));
				}
				Question qTmp = new Question(questionJson.getString("question"),choices);
				questions.add(qTmp);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return new ActCard(name, questions);
	}
}